-
Notifications
You must be signed in to change notification settings - Fork 20
/
ToggleJsxComment.py
286 lines (228 loc) · 8.93 KB
/
ToggleJsxComment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Licensed under the Apache License, Version 2.0 (the “License”); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
from Naomi.plugin.region import (
expand_partial_comments_with_jsx,
expand_partial_lines,
generate_comment_punctuation_region,
generate_jsjsx_comment_punctuation_region,
scan_reverse,
search_non_whitespace,
trim_region
)
from Naomi.plugin.region.scan.predicates import (
has_scope_predicate,
not_predicate
)
from Naomi.plugin.view import (
is_comment, is_whitespace,
is_jsx_open_brace,
is_jsx_close_brace
)
from sublime import Region
from sublime_plugin import TextCommand
import sys
def can_comment(view, region):
region = region.begin()
scopes = view.scope_name(region)
while 'string' in scopes:
region = region - 1
if region < 0:
return False
if view.substr(region) == '\n':
return False
if 'punctuation.definition.string.begin' not in scopes:
return True
return True
def comment_block(view, edit, region):
begin = region.begin()
end = region.end()
empty_line = False
# Added to ensure that the cursor won’t be moved after the comment.
if begin == end:
view.replace(edit, Region(end), '!')
empty_line = True
comment_type = resolve_required_comment_type(view, begin)
if comment_type == 'jsx':
if empty_line:
view.insert(edit, end + 1, ' */}')
view.erase(edit, Region(end, end + 1))
else:
view.insert(edit, end, ' */}')
view.insert(edit, begin, "{/* ")
else:
if empty_line:
view.insert(edit, end + 1, ' */')
view.erase(edit, Region(end, end + 1))
else:
view.insert(edit, end, ' */')
view.insert(edit, begin, "/* ")
def comment_lines(view, edit, region):
lines = view.lines(region)
# Calculate the margin.
margin = sys.maxsize
for line in lines:
non_whitespace_pos = search_non_whitespace(view, line)
line_begin = line.begin()
margin = min(
non_whitespace_pos - line_begin,
margin
)
# Analyse the type of comment that must be used for the entire block.
first_line = lines[0]
comment_type = resolve_required_comment_type(view, first_line.begin())
# Comment.
for line in reversed(lines):
begin = line.begin() + margin
end = max(line.end(), begin)
empty_line = False
# Added to ensure that the cursor won’t be moved after the comment.
if begin == end:
view.replace(edit, Region(end), '!')
empty_line = True
if comment_type == 'jsx':
# JSX comment.
if empty_line:
view.insert(edit, end + 1, ' */}')
view.erase(edit, Region(end, end + 1))
else:
view.insert(edit, end, ' */}')
view.insert(edit, begin, "{/* ")
else:
# Normal JS comment.
view.insert(edit, begin, "// ")
if empty_line:
view.erase(edit, Region(begin + 3, begin + 4))
def must_comment(view, region):
""" Returns true if the region must be commented or not. """
non_whitespace_pos = search_non_whitespace(
view, region, stop_on_line_feed=False
)
# The entire line is blank, a comment must be inserted in the region.
if is_whitespace(view, non_whitespace_pos):
return True
# If it is the JSX open brace, we just need to check the next character.
if is_jsx_open_brace(view, non_whitespace_pos):
non_whitespace_pos += 1
# If the cursor is at a comment, it means that the user wants to remove it.
return not is_comment(view, non_whitespace_pos)
def resolve_required_comment_type(view, offset):
"""
Returns the comment type that must be applied calculed at the offset.
"""
scopes = view.scope_name(offset)
unfenced_scopes = ['source.jsx', 'punctuation.definition.tag.begin']
unfenced_tag = all(x in scopes for x in unfenced_scopes) and \
'meta.jsx-fence' not in scopes
meta_tag_scopes = ['source.jsx', 'meta.tag']
meta_tag = all(x in scopes for x in meta_tag_scopes)
comment_type = 'js'
if unfenced_tag or ('source.jsx' in scopes and not meta_tag):
comment_type = 'jsx'
return comment_type
def uncomment_region(view, edit, region):
begin = region.begin()
end = region.end() - 1
# We will loop backwards, this means that it will hit the closing
# punctuation for block comments first.
i = end + 1
while i > begin:
i -= 1
scopes = view.scope_name(i)
# Not a punctuation, ignore it.
if 'punctuation.definition.comment' not in scopes:
continue
# Found the second forward slash for the “// ” comment.
if 'comment.line' in scopes:
punctuation_region = generate_comment_punctuation_region(view, i)
view.erase(edit, punctuation_region)
i = punctuation_region.begin()
continue
# We found the beginning of the block comment first, this means that
# there’s no end to it and we can easily remove it. It can be “/* ”,
# “/** ”, “{/* ” or “{/** ”.
if 'punctuation.definition.comment.begin' in scopes:
punctuation_region = generate_jsjsx_comment_punctuation_region(
view, i
)
view.erase(edit, punctuation_region)
i = punctuation_region.begin()
continue
# We are looping backwards, so it is expected to find the closing
# punctuation first which can be “ */” or “ */}”.
possible_jsx_comment = False
if i < view.size() and is_jsx_close_brace(view, i + 1):
possible_jsx_comment = True
closing_punctuation_region = generate_comment_punctuation_region(
view, i
)
# Move the cursor 1 character after the beginning punctuation.
i = scan_reverse(view, i, not_predicate(has_scope_predicate(
'punctuation.definition.comment.begin'
)))
open_punctuation_region = generate_comment_punctuation_region(
view, i - 1
)
# Correct the regions to include the JSX braces if necessary.
if possible_jsx_comment:
if is_jsx_open_brace(view, open_punctuation_region.begin() - 1):
open_punctuation_region = Region(
open_punctuation_region.begin() - 1,
open_punctuation_region.end()
)
closing_punctuation_region = Region(
closing_punctuation_region.begin(),
closing_punctuation_region.end() + 1
)
view.erase(edit, closing_punctuation_region)
view.erase(edit, open_punctuation_region)
# Move the cursor to the beginning of the block to “consume” it.
i = open_punctuation_region.begin()
class NaomiToggleJsxCommentCommand(TextCommand):
""" Actual command to toggle the comment lines and blocks. """
def __init__(self, view):
self.view = view
def run(self, edit, block):
view = self.view
# Expand regions.
expanded_regions = []
for region in self.view.sel():
region = expand_partial_comments_with_jsx(view, region)
if block:
region = trim_region(view, region)
else:
region = expand_partial_lines(view, region)
region = expand_partial_comments_with_jsx(view, region)
expanded_regions.append(region)
# Consolidate regions.
consolidated_regions = []
previous_region = None
for region in expanded_regions:
if previous_region is None:
previous_region = region
continue
if previous_region.intersects(region):
previous_region.cover(region)
continue
consolidated_regions.append(previous_region)
previous_region = region
consolidated_regions.append(previous_region)
# Toggle comments.
for region in reversed(consolidated_regions):
if not must_comment(view, region):
uncomment_region(view, edit, region)
continue
if not can_comment(view, region):
continue
if block:
comment_block(view, edit, region)
else:
comment_lines(view, edit, region)